package com.tusuo.stds.di.module;

import android.app.Application;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.tusuo.stds.Constants;
import com.tusuo.stds.MyApp;
import com.tusuo.stds.biz.CurrentUser;
import com.tusuo.stds.rest.Api;
import com.tusuo.stds.rest.ApiHelper;
import com.tusuo.stds.rest.GetCookiesInterceptor;
import com.tusuo.stds.util.DoubleClickExitDetector;
import com.tusuo.stds.util.Utils;

import java.util.concurrent.TimeUnit;

import javax.inject.Singleton;

import autodagger.AutoExpose;
import dagger.Module;
import dagger.Provides;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author J.Huang
 * @version 1.0
 */
@Module
public class AppModule {
    private Application mApp;

    public AppModule(Application app) {
        mApp = app;
    }

    @Provides
    @AutoExpose(MyApp.class)
    @Singleton
    SharedPreferences provideSharedPreferences() {
        return PreferenceManager.getDefaultSharedPreferences(mApp);
    }

    @Provides
    @AutoExpose(MyApp.class)
    @Singleton
    Api provideApi(SharedPreferences sharedPreferences) {
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
        httpClientBuilder
                // 连接超时时间设置
                .connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                // 读取超时时间设置
                .readTimeout(30, TimeUnit.SECONDS)
                // 失败重试
                .retryOnConnectionFailure(true)
                // TODO uncomment it
                .addInterceptor(httpLoggingInterceptor)
                .addInterceptor(new GetCookiesInterceptor(sharedPreferences))
//                .addInterceptor(httpInterceptor)
        ;

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.ENDPOINT)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .client(httpClientBuilder.build())
                .build();

        return retrofit.create(Api.class);
    }

    @Provides
    @AutoExpose(MyApp.class)
    @Singleton
    ApiHelper provideApiHelper(Api api, SharedPreferences sharedPreferences) {
        String deviceID = Utils.Device.getDeviceUniqueId(mApp);
        int version = Utils.App.getAppVersionCode();
        return new ApiHelper(api, sharedPreferences, deviceID, version);
    }

    @Provides
    @AutoExpose(MyApp.class)
    @Singleton
    CurrentUser provideCurrentUser(SharedPreferences sharedPref) {
        return new CurrentUser(sharedPref);
    }

    @Provides
    @AutoExpose(MyApp.class)
    @Singleton
    DoubleClickExitDetector provideDoubleClickExitDetector() {
        return new DoubleClickExitDetector(mApp);
    }
}
